﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class CTSach : DevComponents.DotNetBar.Controls.SlidePanel
    {
        #region Bien
        DataTable _tableSach = new DataTable();
        DataTable _tableCuonSach = new DataTable();

        Sach _sach = new Sach();
        SqlSach _sqlSach = new SqlSach();

        CuonSach _cuonSach = new CuonSach();
        SqlCuonSach _sqlCuonSach = new SqlCuonSach();
        #endregion

        public CTSach()
        {
            InitializeComponent();
            butDocGiaMuon.Enabled = false;
            dgvCuonSach.SelectionChanged += new EventHandler(dgvCuonSach_SelectionChanged);
        }

        void dgvCuonSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCuonSach.Rows.Count > 0)
            {
                if (dgvCuonSach["TinhTrang", dgvCuonSach.CurrentRow.Index].Value.ToString() == "0")
                    butDocGiaMuon.Enabled = true;
                else
                    butDocGiaMuon.Enabled = false;
            }
        }

        public void UpdateSize()
        {
            this.Size = new Size(((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size.Width - 20,
                                    ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size.Height - 40);
        }        

        private void loadTableSach()
        {
            _tableSach = _sqlSach.GetTable();
            dgvSach.DataSource = _tableSach;            
        }

        private void createTableSach()
        {
            loadTableSach();

            dgvSach.Columns["MaSach"].HeaderText = "Mã sách";
            dgvSach.Columns["MaDauSach"].Visible = false;
            dgvSach.Columns["TenDauSach"].HeaderText = "Tên sách";
            dgvSach.Columns["MaTheLoai"].Visible = false;
            dgvSach.Columns["TenTheLoai"].HeaderText = "Thể loại";
            dgvSach.Columns["MaTacGia"].Visible = false;
            dgvSach.Columns["TenTacGia"].HeaderText = "Tác giả";
            dgvSach.Columns["NhaXuatBan"].HeaderText = "Nhà xuất bản";
            dgvSach.Columns["NamXuatBan"].HeaderText = "Năm xuất bản";
            dgvSach.Columns["SoLuong"].HeaderText = "Số lượng";
            dgvSach.Columns["GiaTien"].HeaderText = "Giá tiền";
        }

        private void createTableCuonSach()
        {
            DataColumn column = new DataColumn();
            column.ColumnName = "TinhTrang2";
            column.DataType = System.Type.GetType("System.String");
            _tableCuonSach.Columns.Add(column);

            dgvCuonSach.Columns["MaCuonSach"].HeaderText = "Mã cuốn sách";
            dgvCuonSach.Columns["MaSach"].Visible = false;
            dgvCuonSach.Columns["TinhTrang"].Visible = false;
            dgvCuonSach.Columns["TinhTrang2"].HeaderText = "Tình trạng";
        }

        private void loadCuonSach()
        {
            int index = dgvSach.CurrentRow.Index;
            CuonSach cuonSach = new CuonSach();
            cuonSach.MaSach = int.Parse(dgvSach["MaSach", index].Value.ToString());
            _tableCuonSach = _sqlCuonSach.GetById(cuonSach);
            dgvCuonSach.DataSource = _tableCuonSach;

            createTableCuonSach();
            for (int i = 0; i < dgvCuonSach.Rows.Count; i++)
            {
                if (dgvCuonSach["TinhTrang", i].Value.ToString() == "1")
                    dgvCuonSach["TinhTrang2", i].Value = "Chưa mượn";
                else
                    dgvCuonSach["TinhTrang2", i].Value = "Đã mượn";
            }

            txtMaSach.Text = dgvSach["MaSach", index].Value.ToString();
            txtTenSach.Text = dgvSach["TenDauSach",index].Value.ToString();
            txtNhaXB.Text = dgvSach["NhaXuatBan",index].Value.ToString();
            txtNamXB.Text = dgvSach["NamXuatBan",index].Value.ToString();
            txtSoLuong.Text = dgvSach["SoLuong",index].Value.ToString();
            tabCuonSach.Checked = true;
        }

        public void ShowSlide()
        {
            this.IsOpen = true;
            createTableSach();
            UpdateSize();            
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }

        private void butNhapSach_Click(object sender, EventArgs e)
        {
            MainForm._nhapSach.BringToFront();
            MainForm._nhapSach.ShowSlide();
        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            loadTableSach();
        }

        private void DeleteSach()
        {
            int index = dgvSach.CurrentRow.Index;
            if (MessageBox.Show("Bạn có chắc muốn xóa sách này","Cảnh báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
                    == DialogResult.Yes)
            {
                _sach.MaSach = int.Parse(dgvSach["MaSach", index].Value.ToString());
                string result = _sqlSach.Delete(_sach);
                if (result != "Success")
                    MessageBox.Show(result,"Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
                else
                    loadTableSach();
            }            
        }        

        private void butXoaSach_Click(object sender, EventArgs e)
        {
            DeleteSach();
        }

        private void dgvSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            e.Cancel = true;
            DeleteSach();
        }

        private void butChiTiet_Click(object sender, EventArgs e)
        {
            loadCuonSach();
        }

        private void DeleteCuonSach()
        {
            int index = dgvCuonSach.CurrentRow.Index;
            if (MessageBox.Show("Bạn có chắc muốn xóa cuốn sách này", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
            {
                _cuonSach.MaCuonSach = int.Parse(dgvCuonSach["MaCuonSach", index].Value.ToString());
                string result = _sqlCuonSach.Delete(_cuonSach);
                if (result != "Success")
                    MessageBox.Show(result, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {                    
                    loadCuonSach();
                    loadTableSach();
                }
            }
        }
        
        private void butDeleteCuonSach_Click(object sender, EventArgs e)
        {
            DeleteCuonSach();
        }

        private void dgvCuonSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            e.Cancel = true;
            DeleteCuonSach();
        }

        private void ShowDocGiaMuon()
        {

        }

        private void butDocGiaMuon_Click(object sender, EventArgs e)
        {
            CTDocGiaMuonSach form = new CTDocGiaMuonSach(int.Parse(dgvCuonSach["MaCuonSach", dgvCuonSach.CurrentRow.Index].Value.ToString()));
            form.Show();
        }   
    }
}
